package com.kepus.dao;

import com.kepus.common.domainobject.DOPage;
import com.kepus.common.domainobject.userMgr.DOUser;
import com.kepus.common.persistenceobject.usermgr.POUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 巩聪 on 2018/6/15.
 */
@Repository
public class UserMgrDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserMgrDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<DOUser> queryUser(DOPage page)
    {
        Map<String, Object> param = page.getParams();
        String SQL = "select id, userName, sex, role, phone, email, registDate, lastLoginTime from tbl_user where " +
                "userName";
        List<DOUser> lstUser = new ArrayList();

        return lstUser;
    }

    public int createUser(List<DOUser> lstDOUser)
    {
        String SQL = "insert into tbl_user(id, userName, sex, role, phone, email, passwd, registDate, lastLoginTime) " +
                "value(?,?,?,?,?,?,?,?,?)";
        int[] result = jdbcTemplate.batchUpdate(SQL, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                preparedStatement.setString(1,lstDOUser.get(i).getId());
                preparedStatement.setString(2,lstDOUser.get(i).getUserName());
                preparedStatement.setString(3,lstDOUser.get(i).getSex().toString());
                preparedStatement.setString(4,lstDOUser.get(i).getRole().toString());
                preparedStatement.setString(5,lstDOUser.get(i).getPhone());
                preparedStatement.setString(6,lstDOUser.get(i).getEmail());
                preparedStatement.setString(7,lstDOUser.get(i).getPasswd());
                preparedStatement.setLong(8, lstDOUser.get(i).getRegistDate());
                preparedStatement.setLong(9, lstDOUser.get(i).getLastLoginTime());
            }
            @Override
            public int getBatchSize() {
                return lstDOUser.size();
            }
        });
        return result.length;
    }
}
